package ${project.packageName}.${project.dataCenterType}.ctrl;

import java.text.ParseException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotFoundException;
import java.io.IOException;
import org.apache.hadoop.hbase.client.ResultScanner;
import com.alibaba.fastjson.JSONArray;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.Map;
import ${project.packageName}.${project.dataCenterType}.client.HbaseConvertUtil;
import ${project.packageName}.${project.dataCenterType}.client.HbaseOperateClient;
import ${project.packageName}.${project.dataCenterType}.constant.BaseConstant;
import ${project.packageName}.${project.dataCenterType}.entity.JsonResult;
import ${project.packageName}.${project.dataCenterType}.util.ValidateRequestUtil;
import ${project.packageName}.${project.dataCenterType}.util.DateVerificationUtil;
@RestController
@RequestMapping("/${project.version}")
public class ${ctrlName}Controller {
    
    @Autowired
    private  HbaseOperateClient hbaseOperateClient;
    
    /**
     * 日志对象
     */
    private static final Logger LOG = LoggerFactory.getLogger(${ctrlName}Controller.class);
    
     /**
     * Description: ${restApi.description}
     <#list paraMaps as para>
     * @param ${para.name}
     </#list>
     */
    @RequestMapping(value = "${restApi.requestUrl}", method = RequestMethod.${restApi.requestType})
    public Object ${methodName}( HttpServletRequest request<#list paraMaps as para>, @RequestParam(required=${para.isMust}) ${para.type} ${para.name}</#list> ){
         if (!ValidateRequestUtil.validate(request)) {
            return new JsonResult(BaseConstant.CODE_TOKEN_ILLEGAL, BaseConstant.MSG_TOKEN_ILLEGAL);
        }
        
        try {
        
        
        Map<String, Integer> columnMap = new HashMap<String, Integer>();
        <#list returnValMaps as returnVal>
        columnMap.put("${returnVal.name}", ${returnVal.cellName});
        </#list>
        
      
        JSONArray returnArrayResult = new JSONArray();
        <#if restApi.tableFlag=="yyyyMM">
         List<String> monthBetween = DateVerificationUtil.getMonthBetween(startTime, endTime);
          
         for (String monthStr : monthBetween) {
                String tableName = "${restApi.tableName}" + monthStr;

                // 添加对start、end的再定义，根据当前的表名,作为更精准的定位RowKey
         ${startRowkeyStatements}
         ${endRowkeyStatements}
         ResultScanner resultScanner = hbaseOperateClient.getResultScanner(start, end,
                    tableName);
                JSONArray returnArray = HbaseConvertUtil.resultScan2JSONArray(columnMap,
                    resultScanner,"${familyName}", "${qualifierName}");
                  returnArrayResult.addAll(returnArray);

            }
         <#else>
           String tableName = "${restApi.tableName}" + operationTime.substring(0, 6);
           ${startRowkeyStatements}
           ${endRowkeyStatements}
           ResultScanner resultScanner = hbaseOperateClient.getResultScanner(start, end,
                tableName);
           JSONArray returnArray = HbaseConvertUtil.resultScan2JSONArray(columnMap,
              resultScanner,"${familyName}", "${qualifierName}");
              returnArrayResult.addAll(returnArray);
         </#if>
         
          <#if restApi.returnValIsPage??>
              <#if restApi.returnValIsPage=="true">
                <#include "/java/hbase/ctrl/page.ftl"/>
              </#if>
          <#else>
                return JSON.toJSONString(returnArrayResult);
          </#if>
          
        }
        catch (NumberFormatException e) {
            LOG.error("报错信息：", e);
            return new JsonResult(BaseConstant.CODE_ERROR, BaseConstant.MSG_ERROR);
        }
        catch (IOException e) {
            LOG.error("报错信息：", e);
            return new JsonResult(BaseConstant.CODE_ERROR, BaseConstant.MSG_ERROR);
        }
        <#if restApi.tableFlag=="yyyyMM">
        catch (ParseException e) {
            LOG.error("报错信息：", e);
            return new JsonResult(BaseConstant.CODE_DATA_NONEXIST, BaseConstant.MSG_DATA_NONEXIST);
        }
        </#if>
    }
}
